package com.site_assembly.config;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class RabbitCosumeConfig {

        @Resource
        private CachingConnectionFactory cachingConnectionFactory;

        @Bean
        public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(){
            SimpleRabbitListenerContainerFactory containerFactory = new SimpleRabbitListenerContainerFactory();
            //连接工厂
            containerFactory.setConnectionFactory(cachingConnectionFactory);
            // ACK模式
            containerFactory.setAcknowledgeMode(AcknowledgeMode.AUTO);
            // 预取数量
            containerFactory.setPrefetchCount(1);
            //并发消费者数量
            containerFactory.setConcurrentConsumers(1);
            //最大的并发消费者数量
            containerFactory.setMaxConcurrentConsumers(1);
            //设置使用json反序列化
            containerFactory.setMessageConverter(new Jackson2JsonMessageConverter());
            return containerFactory;
        }

}

